﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.Management.Pipe
{
    /// <summary>
    /// A collection of pipevalue objects.
    /// </summary>
    [Serializable]
    public class PipeValueCollection
    {
        /// <summary>
        /// The list of pipevalues.
        /// </summary>
        private List<PipeValue> values;

        /// <summary>
        /// Initializes a new instance of the <see cref="PipeValueCollection"/> class.
        /// </summary>
        public PipeValueCollection()
        {
            this.values = new List<PipeValue>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PipeValueCollection"/> class.
        /// </summary>
        /// <param name="values">A list of pipevalues.</param>
        public PipeValueCollection(List<PipeValue> values)
        {
            this.values = new List<PipeValue>();

            for (int i = 0; i < this.values.Count; i++)
            {
                this.values.Add(values[i]);
            }
        }

        /// <summary>
        /// Gets or sets the values of the pipecollection.
        /// </summary>
        /// <value>The values of the pipecollection.</value>
        public List<PipeValue> Values
        {
            get { return values; }
            set { values = value; }
        }

        /// <summary>
        /// Gets the <see cref="SmartMathLibrary.Management.Pipe.PipeValue"/> with the specified search value.
        /// </summary>
        /// <value>The name of the pipevalue.</value>
        public PipeValue this[string searchValue]
        {
            get { return this.FindPipeValueByName(searchValue); }
        }

        /// <summary>
        /// Finds the pipevalue of the current pipecollection by its name.
        /// </summary>
        /// <param name="compareName">The name of the searched pipevalue.</param>
        /// <returns>The pipevalue object with the specified pipevalue name.</returns>
        public PipeValue FindPipeValueByName(String compareName)
        {
            for (int i = 0; i < this.values.Count; i++)
            {
                if (this.values[i].Name.Equals(compareName))
                {
                    return this.values[i];
                }
            }

            throw new PipeValueNotFoundException("A pipevalue with this name cannot be found.");
        }

        /// <summary>
        /// Checks it the specified pipevalue name exists in the current collection.
        /// </summary>
        /// <param name="compareName">The specified pipevalue name.</param>
        /// <returns>True, if the current pipevalue name exists otherwise, false.</returns>
        public bool PipeValueNameExists(String compareName)
        {
            for (int i = 0; i < this.values.Count; i++)
            {
                if (this.values[i].Name.Equals(compareName))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Gets the enumerator.
        /// </summary>
        /// <returns>The enumerator of the current iteration.</returns>
        public IEnumerator<PipeValue> GetEnumerator()
        {
            for (int i = 0; i < this.values.Count; i++)
            {
                yield return this.values[i];
            }
        }
    }
}